package com.wondertek.liveroom;

import java.util.ArrayList;
import java.util.List;

import com.jasontools.data.ChatEmoji;
import com.jasontools.utils.ChatEmojiConvertUtil;
import com.viewpagerindicator.CirclePageIndicator;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

public class FragmentChatEmoji extends Fragment implements OnItemClickListener {
	private static final String TAG = "FragmentChatEmoji";
	private View view = null;
	private ViewPager vpEmoji = null;
	private CirclePageIndicator pageIndicator = null;
	private ChatEmojiPagerAdapter adapter = null;
	private int curEmojiPageIndex = 0;
	
	private EditText etMsg = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_chat_emoji, container, false);
		initCtrl();
		return view;
	}
	
	private void initCtrl() {
		this.vpEmoji = (ViewPager) view.findViewById(R.id.vp_chat_emoji);
		this.pageIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator_chat_emoji);
		//createChatEmojiGridView();
		if (ChatEmojiConvertUtil.getInstance().getListPageEmoji().size() == 0) {
			ChatEmojiConvertUtil.getInstance().getAssetFileText(getActivity());
		}		
		this.adapter = new ChatEmojiPagerAdapter(getActivity(), createChatEmojiGridView());
		this.vpEmoji.setAdapter(adapter);
		this.pageIndicator.setViewPager(vpEmoji);
		
//		this.vpEmoji.setOnPageChangeListener(new OnPageChangeListener() {
//			@Override
//			public void onPageScrollStateChanged(int arg0) {	
//			}
//			@Override
//			public void onPageScrolled(int arg0, float arg1, int arg2) {
//			}
//			@Override
//			public void onPageSelected(int pageIndex) {
//				curEmojiPageIndex = pageIndex;
//			}			
//		});
	}
	
	private void setMsgEditText() {
		this.etMsg = ((LiveActivity)getActivity()).getMsgEditText();
	}
	
	private List<View> createChatEmojiGridView() {
		List<View> list = new ArrayList<View>();
		for(List<ChatEmoji> tempList : ChatEmojiConvertUtil.getInstance().getListPageEmoji()) {
			list.add(this.createChatEmojiGridView(tempList));
		}
		Log.e(TAG, "list size: " + list.size());
		return list;
	}
	
	private GridView createChatEmojiGridView(List<ChatEmoji> data) {
		GridView gridView = new GridView(this.getActivity());
		ChatEmojiGridViewAdapter tempAdapter = new ChatEmojiGridViewAdapter(this.getActivity(), data);
		gridView.setAdapter(tempAdapter);
		gridView.setOnItemClickListener(this);  
		gridView.setNumColumns(7);  
		gridView.setBackgroundColor(Color.TRANSPARENT); 
		//gridView.setBackgroundColor(Color.rgb(255, 0, 0)); 
		gridView.setHorizontalSpacing(1);  
		gridView.setVerticalSpacing(50);  
		gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); 
		//gridView.setStretchMode(GridView.STRETCH_SPACING); 
		gridView.setCacheColorHint(0);  
		gridView.setPadding(5, 0, 5, 0);  
		gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));  
		gridView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,  
                LayoutParams.MATCH_PARENT));  
		gridView.setGravity(Gravity.CENTER);
		return gridView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		this.curEmojiPageIndex = this.vpEmoji.getCurrentItem();
		setMsgEditText();
		Log.e(TAG, "you clicked:   " + position);
		ChatEmoji emoji = (ChatEmoji) ChatEmojiConvertUtil.getInstance().getListPageEmoji().get(curEmojiPageIndex).get(position);
		if (emoji == null) {
			return;
		}
		int resId = emoji.getEmojiId();
		if(resId == R.drawable.clear_emoji) {
			int selection = etMsg.getSelectionStart();
			String strText = etMsg.getText().toString();
			if (selection > 0) {
				String strText2 = strText.substring(selection - 1);
				if ("]".equals(strText2)) {
					int start = strText.lastIndexOf("[");
					int end = selection;
					etMsg.getText().delete(start, end);
					return;
				}
				etMsg.getText().delete(selection - 1, selection);
			}
		}
		if (!TextUtils.isEmpty(emoji.getEmojiCharactor())) {  
//            if (mListener != null)  
//                mListener.onCorpusSelected(emoji);  
            SpannableString spannableString = ChatEmojiConvertUtil.getInstance()  
                    .addFace(getActivity(), emoji.getEmojiId(), emoji.getEmojiCharactor());  
            etMsg.append(spannableString);  
        }
	}
}

class ChatEmojiGridViewAdapter extends BaseAdapter {
	private Context context;
	private List<ChatEmoji> listData;
	
	public ChatEmojiGridViewAdapter(Context context, List<ChatEmoji> list) {
		this.context = context;
		this.listData = list;
	}

	@Override
	public int getCount() {
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(context).inflate(R.layout.item_chat_emoji_gridview, null);
		ImageView ivIcon = (ImageView) convertView.findViewById(R.id.iv_item_chat_emoji);
		ivIcon.setBackgroundResource(listData.get(position).getEmojiId());
		return convertView;
	}
}

class ChatEmojiPagerAdapter extends PagerAdapter {
	private Context context;
	private List<View> listData;
	
	public ChatEmojiPagerAdapter(Context context, List<View> list) {
		this.context = context;
		this.listData = list;
	}

	@Override
	public int getCount() {
		return listData.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}
	
	@Override  
    public int getItemPosition(Object object) {  
        return super.getItemPosition(object);  
    }  
  
    @Override  
    public void destroyItem(View arg0, int arg1, Object arg2) {  
        ((ViewPager) arg0).removeView(listData.get(arg1));  
    }  
  
    @Override  
    public Object instantiateItem(View arg0, int arg1) {  
        ((ViewPager) arg0).addView(listData.get(arg1));  
        return listData.get(arg1);  
    }
}
